<?php
    Class User_model extends CI_Model
    {
        public function __construct()
        {
          parent::__construct();
        }

       function login($username, $password)
       {
         $this -> db -> select('id, username, password');
         $this -> db -> from('users');
         $this -> db -> where('username', $username);
         $this -> db -> where('password', MD5($password));
         $this -> db -> where('active', 1);
         $this -> db -> limit(1);

         $query = $this -> db -> get();

         if($query -> num_rows() == 1)
         {
           return $query->result();
         }
         else
         {
           return false;
         }
       }

       function listUsers($search = false)
       {
          // retrieve all users except admin
          $this->db->select('*');
          $this->db->from('users');
          $this->db->where('id !=',1);  // except admin

          if(!empty($search))
          {
            if(!empty($search['username']))
            {
              $this->db->like('username',$search['username']);
            }
            if(!empty($search['user_cat']) && ($search['user_cat'] != 'all'))
            {
              $this->db->where('user_cat',$search['user_cat']);
            }
          }

          $query = $this->db->get();

          return $query->result();
       }

       function getUser($id)
       {
        $query = $this->db->get_where('users', array('id'=>$id));
        return $query->row();
       }

       function userInfo($username = false)
       {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('username', $username);
        $this->db->limit(1);

        $query = $this->db->get();
        return $query->row();
       }

       public function getUserInfo($search_data) 
       {
          $this->db->select('fullname');
          $this->db->select('id');
          $this->db->like('fullname', $search_data);
          return $this->db->get('users', 5);
       }

       function insert($data)
       {  
          $this->db->insert('users',$data);

          return $this->db->insert_id();
       }  

       function update($data,$id)
       {
        $this->db->where('id',$id);
        $this->db->update('users',$data);
       }

       function retrieveUser($username = false)
       {
          $this->db->select('*');
          $this->db->from('users');
          $this->db->like('username', $username);

          $query = $this->db->get();

          echo $this->db->last_query();

          return $query->row();
       }
    }
?>
